using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Common.Functions
{
    public class ConvertBinaryHex
    {
        public static string hexToBinary(string hex, int clmLength)
        {
            StringBuilder binaryStb = new StringBuilder();
            for (int i = 0; i < hex.Length; i++)
            {
                int intTen = Convert.ToInt32(hex.Substring(i, 1), 16);
                string tempStr = Convert.ToString(intTen, 2);
                tempStr = tempStr.PadLeft(4, '0');
                binaryStb.Append(tempStr);
            }
            string binary = binaryStb.ToString();
            if (clmLength - hex.Length > 0)
            {
                binary = binary.PadRight(clmLength * 4, '0');
            }
            return binary;
        }

        public static string hexToBinary(string hex)
        {
            string trimHex = hex.Trim();
            StringBuilder binaryStb = new StringBuilder();
            for (int i = 0; i < trimHex.Length; i++)
            {
                int intTen = Convert.ToInt32(trimHex.Substring(i, 1), 16);
                string tempStr = Convert.ToString(intTen, 2);
                tempStr = tempStr.PadLeft(4, '0');
                binaryStb.Append(tempStr);
            }
            string binary = binaryStb.ToString();
            if (hex.Length - trimHex.Length > 0)
            {
                binary = binary.PadRight(hex.Length * 4, '0');
            }
            return binary;
        }

        public static string binaryToHex(string binary, int clmLength)
        {
            int length;
            if (binary.Length > clmLength * 4)
            {
                length = binary.Length % 4 == 0 ? (binary.Length / 4) : (binary.Length / 4 + 1);
            }
            else
            {
                length = clmLength;
            }
            string strBin = binary.PadRight(length * 4, '0');
            StringBuilder hex = new StringBuilder();
            int start = 0;
            for (int count = 0; count < length; count++)
            {
                int intTen = Convert.ToInt32(strBin.Substring(start, 4), 2);
                hex.Append(Convert.ToString(intTen, 16));
                start += 4;
            }
            return hex.ToString();
        }

        public static string bytesToHex(byte[] byteArr)
        {
            StringBuilder hex = new StringBuilder();
            for (int i = 0; i < byteArr.Length; i++)
            {
                string temp = Convert.ToString(byteArr[i], 16);
                hex.Append(temp.PadLeft(2, '0'));
            }
            return hex.ToString();
        }

        public static byte[] HexToBytes(string hex)
        {
            int length = hex.Length / 2;
            byte[] retByte = new byte[length];
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                retByte[i] = Convert.ToByte(hex.Substring(index, 2), 16);
                index += 2;
            }
            return retByte;
        }

        public static string StringToHex(string str)
        {
            byte[] stringByte = Encoding.UTF8.GetBytes(str);
            return bytesToHex(stringByte);
        }

        public static string HexToString(string hex)
        {
            byte[] stringByte = HexToBytes(hex);
            return Encoding.UTF8.GetString(stringByte);
        }

        public static string Decrypt(string strText, string strDecrKey)
        {
            byte[] byKey = null;
            byte[] IV = { 0x34, 0xEF, 0x12, 0x56, 0xAB, 0x78, 0xCD, 0x90 };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(strDecrKey.Substring(0,8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = new System.Text.UTF8Encoding();
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                //CommonLogFunction.AddLogMsg(CommonLogFunction.LOG_TYPE.EXCEPTION, ex);
                return string.Empty;
            }
        }

        public static string Encrypt(string strText, string strEncrKey)
        {
            byte[] byKey = null;
            byte[] IV = { 0x34, 0xEF, 0x12, 0x56, 0xAB, 0x78, 0xCD, 0x90 };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                //CommonLogFunction.AddLogMsg(
                //    CommonLogFunction.LOG_TYPE.EXCEPTION, ex);
                return string.Empty;
            }
        }

    }
}
